package demoMod.plugins.mod.sts.lineMarkers;

import com.intellij.codeInsight.daemon.LineMarkerInfo;
import com.intellij.codeInsight.daemon.LineMarkerProvider;
import com.intellij.openapi.editor.markup.GutterIconRenderer;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.PsiClassImpl;
import demoMod.plugins.mod.sts.GlobalStatus;
import demoMod.plugins.mod.sts.Icons;
import demoMod.plugins.mod.sts.lineMarkers.handlers.ModCoreNavigationHandler;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.List;

public class ModCoreLineMarker implements LineMarkerProvider {
    @Override
    public LineMarkerInfo<?> getLineMarkerInfo(@NotNull PsiElement psiElement) {
        LineMarkerInfo<PsiMethod> lineMarkerInfo= null;
        try {
            lineMarkerInfo = null;
            String anno="com.evacipated.cardcrawl.modthespire.lib.SpireInitializer";
            if(!judgeHaveAnnotation(psiElement,anno)){
                return lineMarkerInfo;
            }
            PsiClassImpl modClass = ((PsiClassImpl) psiElement);
            PsiMethod[] methods = modClass.getMethods();
            PsiMethod psiMethod = null;
            for (PsiMethod method : methods) {
                if (method.getModifierList().hasModifierProperty("public") &&
                    method.getModifierList().hasModifierProperty("static") &&
                    method.getReturnType().equals(PsiType.VOID) &&
                    method.getName().equals("initialize") &&
                    method.getParameterList().isEmpty()) {
                    psiMethod = method;
                    break;
                }
            }
            if (psiMethod == null) return null;
            lineMarkerInfo = new LineMarkerInfo<>(
                    psiMethod,
                    psiMethod.getTextRange(),
                    Icons.MOD_ENTRY,
                    a -> "这是mod的入口点",
                    new ModCoreNavigationHandler(),
                    GutterIconRenderer.Alignment.LEFT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return lineMarkerInfo;
    }

    @Override
    public void collectSlowLineMarkers(@NotNull List<? extends PsiElement> elements, @NotNull Collection<? super LineMarkerInfo<?>> result) {

    }

    private boolean judgeHaveAnnotation(@NotNull PsiElement psiElement, String anno) {
        if (psiElement instanceof PsiClass) {
            PsiClassImpl field = ((PsiClassImpl) psiElement);
            PsiAnnotation psiAnnotation = field.getAnnotation(anno);
            return null != psiAnnotation;
        }
        return false;
    }
}
